package com.gxaedu.dly2022.advice;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author thinknovo
 * @version 1.0
 */
@RestControllerAdvice
public class MyControllerAdvice {
    @ExceptionHandler(NumberFormatException.class)
    public Map<String, Object> checkException01(Exception e) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 501);
        map.put("msg", "对不起，请输入纯整数数字");
        return map;
    }

    @ExceptionHandler(FileNotFoundException.class)
    public Map<String, Object> checkException02(Exception e) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 501);
        map.put("msg", "对不起，文件路径没有被找到");
        return map;
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Map<String, Object> checkException03(Exception e) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 501);
        map.put("msg", "对不起，数组的下标超界");
        return map;
    }

//    @ExceptionHandler(MySQLIntegrityConstraintViolationException.class)
//    public Map<String, Object> checkException04(Exception e) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 501);
//        map.put("msg", "数据库  主键或者唯一键重复");
//        return map;
//    }

    @ExceptionHandler(MySQLSyntaxErrorException.class)
    public Map<String, Object> checkException05(Exception e) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 501);
        map.put("msg", "数据库  找不到指定的数据库字段");
        return map;
    }
}
